"""
自定义的分页组件,以后如果想要使用这个分页组件
在视图函数中
def phonenum_list(request):
    #根据自己的情况筛选自己的数据
    queryset=PhoneNumber.objects.filter(**data_dic).order_by("-level")
    #实例化分页对象
    page_object=Pagination(request,queryset)
    context={'phonelist':page_object.page_queryset,#分页后的数据
             'page_string':page_object.html()}#生成的页码
    return render(request,'phone_list.html',context)返回到HTML中
在HTML页面中
{% for item in phonelist %}
          <tr>
            <th scope="row">{{ item.id }}</th>
            <td>{{ item.mobilenumber }}</td>
            <td>{{ item.get_level_display}}</td>
            <td>{{ item.get_status_display }}</td>
            <td>{{ item.price }}</td>
            <td>{{ item.address }}</td>
          </tr>
        {% endfor %}
<ul class="pagination" style="float:left;">
      {{ page_string }}
</ul>
"""
from django.utils.safestring import mark_safe
from django.utils.safestring import mark_safe
class Pagination(object):
    def __init__(self,request,queryset,page_size=10,page_param="page",plus=5):
        """
        :param request: 请求的对象
        :param queryset: 查询的符合条件的数据（根据这个数据进行分页处理）
        :param page_size:每页显示的数据
        :param page_param:在URL中传递的获取分页的参数例如?page=12
        :param plus:显示当前页的前或后几页
        """
        from django.http.request import QueryDict
        import copy
        self.page_param=page_param
        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True
        self.query_dict=query_dict

        self.query_dict=query_dict
        page=(request.GET.get(page_param,'1'))
        if page.isdecimal():#判断是否是十进制的数
            page=int(page)
        else:
            page=1
        print(page,type(page))
        self.page=page
        self.page_size=page_size
        self.start=(page-1)*self.page_size
        self.end=(page)*self.page_size
        self.plus=plus
        self.page_queryset=queryset[self.start:self.end]

        total_count = queryset.count()
        total_page_count, div = divmod(total_count,page_size)
        if div:
            total_page_count += 1
        self.total_page_count=total_page_count
    def html(self):
        # plus = 5
        if self.total_page_count <= 2 * self.plus:
            # 数据库中数据比较少那就全部显示
            start_page = 1
            end_page = self.total_page_count + 1
        else:
            # 数据库中的数据比较多
            # 当前页小于5
            if self.page <= self.plus:
                start_page = 1
                end_page = self.plus * 2
            else:
                # 当前页>5
                # 当前页+5>总页码
                if (self.page + self.plus) > self.total_page_count:
                    start_page = self.total_page_count - 2 * self.plus
                    end_page = self.total_page_count
                else:
                    start_page = self.page - self.plus
                    end_page = self.page + self.plus + 1

        page_str_list = []
        self.query_dict.setlist(self.page_param, [1])
        print(self.query_dict.urlencode())
        # 首页
        start1 = '<li ><a href="?page={}">首页</a></li>'.format(self.query_dict.urlencode())
        page_str_list.append(start1)
        # 上一页
        if self.page > 1:
            self.query_dict.setlist(self.page_param, [self.page - 1])
            print(self.query_dict.urlencode())
            prev = '<li ><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())

        else:
            self.query_dict.setlist(self.page_param, [1])
            print(self.query_dict.urlencode())
            prev = '<li ><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())
        page_str_list.append(prev)
        # 下一页

        for i in range(start_page, end_page + 1):
            if i == self.page:
                self.query_dict.setlist(self.page_param, [i])
                print(self.query_dict.urlencode())
                ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            else:
                self.query_dict.setlist(self.page_param, [i])
                print(self.query_dict.urlencode())
                ele = '<li><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            page_str_list.append(ele)
        if 0 < self.page < self.total_page_count:
            self.query_dict.setlist(self.page_param, [self.page + 1])
            print(self.query_dict.urlencode())
            next = '<li ><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())
        else:
            self.query_dict.setlist(self.page_param, [self.total_page_count])
            print(self.query_dict.urlencode())
            next = '<li ><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())
        # for i in range(1,total_page_count+1):
        page_str_list.append(next)
        # 尾页
        self.query_dict.setlist(self.page_param, [self.total_page_count])
        print(self.query_dict.urlencode())
        end1 = '<li ><a href="?{}">尾页</a></li>'.format(self.query_dict.urlencode())
        page_str_list.append(end1)
        search_string = """
            <li>
             <form method="get" style="float:left;margin-left:-1px;">
                    <div class="input-group" style="width:300px">

                    <input type="text" class="form-control" placeholder="page..." name="page" style="width:120px">

                    <button class="btn btn-default" type="submit">跳转
                    </button></div>
                            </form>
                            </li>
            """
        page_str_list.append(search_string)
        page_string = mark_safe(''.join(page_str_list))
        return page_string
